package br.edu.unilasalle.simon_api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import br.edu.unilasalle.simon_api.dao.ICondPagtoDao;
import br.edu.unilasalle.simon_api.dao.IFormPagtoDao;
import br.edu.unilasalle.simon_api.dao.IGenericDao;
import br.edu.unilasalle.simon_api.domain.CondicaoPagamento;
import br.edu.unilasalle.simon_api.domain.FormaPagamento;
import br.edu.unilasalle.simon_api.service.ICondPagtoService;

@Service
public class CondPagtoService extends GenericService<CondicaoPagamento, Long> implements ICondPagtoService {
	
	@Autowired
	private ICondPagtoDao dao;
	
	@Autowired
	private IFormPagtoDao formPagtoDao;
	
	public ICondPagtoDao getCondPagtoDao() {
		return dao;
	}
	
	public void setCondPagtoDao(ICondPagtoDao dao) {
		this.dao = dao;
	}

	@Override
	public IGenericDao<CondicaoPagamento, Long> getEntityDao() {
		return this.dao;
	}
    
	@Override
	public List<CondicaoPagamento> findAllCondPagto() {
		return this.dao.findAllCondPagto();
	}
	
	@Override
	public Boolean findByValor(Double valor, Long idFormPagto, Integer parcelas) {
		return this.dao.findByValor(valor, idFormPagto, parcelas);
	}

	@Override
	@Transactional(readOnly = false)
	public CondicaoPagamento save(CondicaoPagamento obj) {
		FormaPagamento aux = this.formPagtoDao.findById(obj.getFormaPagamento().getId());
		if (obj.getId() == null) {
			obj.setFormaPagamento(aux);
		} else {
			obj.getFormaPagamento().setDescricao(aux.getDescricao());
		}
		return this.dao.save(obj);
	}
	
}
